import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-dark.css'
import { marked } from 'marked'
import { markedHighlight } from 'marked-highlight'

// 配置代码高亮
marked.use(
  markedHighlight({
    langPrefix: 'hljs language-',
    highlight(code: string, lang?: string) {
      const language = lang && hljs.getLanguage(lang) ? lang : 'plaintext'
      return hljs.highlight(code, { language }).value
    }
  })
)

// 自定义 renderer 处理 <think> 标签
const renderer = new marked.Renderer()

// 重写 renderer 的代码来处理 <think> 标签
renderer.text = function (data: any) {
  console.log('data', data)
  const { text } = data

  console.log(text, '1111')

  // 查找 <think> 标签并将其内容替换为带样式的 span 标签
  return text.replace(/<think>(.*?)<\/think>/g, (match, p1) => {
    console.log('489484848784874878+784897897987897')
    return `<span style="color:red">${p1}</span>`
  })
}

// 将 renderer 应用到 marked 中
marked.setOptions({
  renderer: renderer
})

export default marked
